/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package io.github.ilkgunel.operations;

import com.mongodb.client.result.UpdateResult;
import io.github.ilkgunel.database.AccessMongoDB;

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.github.ilkgunel.util.util;
import org.bson.Document;
import org.bson.types.ObjectId;

/**
 *
 * @author ilkaygunel
 */
@WebServlet(name = "test4_1", urlPatterns = {"/test4_1"})
public class test4_1 extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String updateMessage = "";

        String data=request.getParameter("data");
        System.out.println(data);
        List<String[]> list= util.JsonToList(data);
        AccessMongoDB accessMongoDB = new AccessMongoDB();

        try {
            System.out.println(list.size());
            for (int i = 1; i <list.size() ; i++){
                String []temp=list.get(i);
                String _id = temp[0].replaceAll("\"","");
                String sid = temp[1].replaceAll("\"","");
                String name = temp[2].replaceAll("\"","");
                String sex = temp[3].replaceAll("\"","");
                int age = Integer.parseInt(temp[4].replaceAll("\"",""));
                String birthday = temp[5].replaceAll("\"","");;
                String dname= temp[6].replaceAll("\"","");;
                String theclass= temp[7].replaceAll("\"","");;


                ObjectId objectId = new ObjectId(_id);


                UpdateResult updateResult = accessMongoDB.getCollection("student").updateOne(new Document("_id", objectId),
                        new Document("$set",new Document()
                                .append("sid",sid )
                                .append("name", name)
                                .append("sex", sex)
                                .append("age",age)
                                .append("birthday",birthday)
                                .append("dname",dname)
                                .append("class",theclass)
                        ));
            }
            updateMessage = "Update Is Successful";
            System.out.println(updateMessage);


        } catch (Exception e) {
            updateMessage = "An error occured while updating record. Error is:"+e;
            System.out.println(updateMessage);

        }

        request.setAttribute("updateMessage", updateMessage);
        accessMongoDB.closeMongoClient();

        RequestDispatcher dispatcher = request.getRequestDispatcher("/test4_1(1).jsp");
        dispatcher.forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        String updateMessage = "";

        String data=request.getParameter("data");
        List<String[]> list= util.JsonToList(data);
        AccessMongoDB accessMongoDB = new AccessMongoDB();

        try {

            for (int i = 1; i <list.size() ; i++){
                String []temp=list.get(i);
                String _id = temp[0];
                String sid = temp[1];
                String name = temp[2];
                String sex = temp[3];
                int age = Integer.parseInt(temp[4]);
                String birthday = temp[5];
                String dname= temp[6];
                String theclass= temp[7];


        ObjectId objectId = new ObjectId(_id);


                UpdateResult updateResult = accessMongoDB.getCollection("student").replaceOne(new Document("_id", objectId),
                        new Document()
                                .append("_id",objectId )
                                .append("sid",sid )
                                .append("name", name)
                                .append("sex", sex)
                                .append("age",age)
                                .append("birthday",birthday)
                                .append("dname",dname)
                                .append("class",theclass)
                );
            }
            updateMessage = "Update Is Successful";
            System.out.println(updateMessage);

        } catch (Exception e) {
            updateMessage = "An error occured while updating record. Error is:"+e;

        }

        request.setAttribute("updateMessage", updateMessage);

        accessMongoDB.closeMongoClient();

        RequestDispatcher dispatcher = request.getRequestDispatcher("/test4_1(1).jsp");
        dispatcher.forward(request, response);
    }
}
